package com.java.controller;


import com.java.pojo.*;
import com.java.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/***
 * 进货订单
 */
@Controller
@RequestMapping("/EnterGood")
public class EnterGood {

    @Autowired
    private EnterGoodService enterGoodService;
    @Autowired
    private EnterGoodsService enterGoodsService;
    @Autowired
    private GoodStyleService goodStyleService;
    @Autowired
    private SuppliersService suppliersService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private StoresService storesService;

    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String s(){
        return "<script>alert('欢迎回来');location.href='/EnterGood/EnterPageGood';</script>";
    }


    //查询全部,分页
    @RequestMapping(value = "/EnterPageGood",produces = "text/html;charset=UTF-8")
    public String page(@RequestParam(value = "start",required = false) Integer start, @RequestParam(value = "document",required = false)String document , Model model){
        if(document==null ||"".equals(document)){
            document="";
        }
        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+document);
        /*计算总页数*/
        int totalCount = enterGoodService.getCount(document);
        int i = totalCount % 4 ==0? totalCount/4 :(totalCount/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }
        Page<com.java.pojo.EnterGood> page =enterGoodService.getPageByPage(start,document);
        System.out.println("page:"+page);
        if(page!=null&&!page.equals("")){
        model.addAttribute("page",page);
        model.addAttribute("document",document);
        return "enterGood/enterGood";
        }else {
            model.addAttribute("page","");
            return "enterGood/enterGood";
        }
    }


    /*进入添加的订货订单页面*/
    @RequestMapping(value = "/addEnterGood", produces = "text/html;charset=UTF-8")
    public String add(Model model){
        List<GoodStyle> goodStyleList = goodStyleService.getGoodStyleList();
        List<Suppliers> suppList = suppliersService.getSuppList();
        List<Repository> reList = repositoryService.getReList();
        List<Stores> storesList = storesService.getStoresList();
        model.addAttribute("goodStyleList",goodStyleList);
        model.addAttribute("suppList",suppList);
        model.addAttribute("reList",reList);
        model.addAttribute("storesList",storesList);
        return "enterGood/addEnterGood";
    }


    /*添加进货订单的操作*/
    @RequestMapping(value = "/addEnterGood2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addEnterGood2(
            @RequestParam(value = "goodStyle",required = false) Integer goodStyle,
            @RequestParam(value = "data1",required = false) String data,
            @RequestParam(value = "forname",required = false) Integer forname,
            @RequestParam(value = "rid",required = false) Integer rid,
            @RequestParam(value = "stores",required = false) Integer stores,
            Model model

    ) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(data); //日期
        com.java.pojo.EnterGood enterGood=new com.java.pojo.EnterGood();
        enterGood.setData1(date1);
        enterGood.setGoodStyle(goodStyle);
        enterGood.setForname(forname);
        enterGood.setRid(rid);
        enterGood.setStores(stores);
        /*编号*/
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf2.format(new Date());
        String result="";
        String s="XSDD";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        System.out.println(s+newDate+result);
        String document=s+newDate+result;
        enterGood.setDocument(document);
        int i = enterGoodService.addEnterGood(enterGood);
        if(i>0){
            return "<script>alert('successful');location.href='/EnterGood/EnterPageGood';</script>";
        }else {
            return "<script>alert('添加失败!');location.href='/EnterGood/EnterPageGood';</script>";
        }
    }


    /*删除*/
    @RequestMapping(value = "/del",produces = "text/html;charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public String  del(@RequestParam(value = "eid",required = false) String eid,Model model){
        System.out.println(eid);
        int i2 = enterGoodService.deleteEnterGood(eid);
        int i = enterGoodsService.deleteEnterGoodByEgid2(eid);
        if(i2>0&&i>0 || i2>0 || i>0){
            return "<script>alert('删除成功');location.href='/EnterGood/EnterPageGood';</script>";
        }else {
            return "<script>alert('删除成功');location.href='/EnterGood/EnterPageGood';</script>";
        }
    }


    /*入库*/
    @RequestMapping(value = "/enter",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String enter(@RequestParam(value = "eid",required = false) String eid){
        System.out.println(eid);
        boolean update = enterGoodService.update(Integer.parseInt(eid));
        if(update){
            return "<script>alert('入库成功!欢迎回来');location.href='/EnterGood/EnterPageGood';</script>";
        }
        return "<script>alert('入库失败!');location.href='/EnterGood/EnterPageGood';</script>";

    }


    /*退货*/
    @RequestMapping(value = "/out",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String out(@RequestParam(value = "eid",required = false) String eid) {
        boolean b = enterGoodService.outIn(Integer.parseInt(eid));
        if(b){
            return "<script>alert('退货successful');location.href='/EnterGood/EnterPageGood';</script>";
        }
        return "<script>alert('还未入库,请先入库!!');location.href='/EnterGood/EnterPageGood';</script>";
    }
}
